import java.util.HashMap;
import java.util.Map;

public class Solution1 {

    /**
     * 1. 遍历整个数组，将遍历完成的值作为key放入hashMap中
     * 2. 如果hashMap中包含 目标值-新遍历的值 就找到了返回下标
     * 3. put(K,V) get(V) = K
     *
     * @param nums
     * @param target
     * @return
     */
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i]))

                return new int[]{map.get(target - nums[i]), i};
            map.put(nums[i], i);
        }
        return new int[2];
    }

    public static void main(String[] args) {
        int[] nums = {1, 3, 5, 4, 5};

        int[] ints = new Solution1().twoSum(nums, 2);
        for (int i : ints) {
            System.out.println(i);
        }
    }
}
